lib.use(['element', 'table', 'form','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var roleTable;
    //初始表格基础参数
    table.set(common.layui.table.config());
    // 渲染表格
    roleTable = table.render({
        elem: '#roleTable'
        , url: common.util.createUrl('/sys/role/list')
        , cols: [[
              {type:'checkbox'}
            , {type: 'numbers', title: '编号'}
            , {field: 'id', title: 'Id', hide:true}
            , {field: 'roleName', title: '角色名称', minwidth: 350}
            , {field: 'isValid', title: '是否有效', width: 80, templet: function (row) {
                return common.dist.getValue("comm.ValidEnum",row.isValid);
            }}
            , {field: 'remark', title: '备注', minwidth: 250}
            , {field: 'updateUserName', title: '更新人', width: 120}
            , {field: 'updateTime', title: '更新时间', width: 150}
            , {
                field:'id', fixed: 'right', title: '操作', width: 140, templet: function (row) {
                    return '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">授权</a>' +
                           '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                }
            }
        ]]
    });

     //监听单元格事件
    table.on('tool(roleTableFilter)', function(obj) {
        if (obj.event === 'edit') {
            location.href = 'roleForm.html?id=' + obj.data.id;
        }
        if (obj.event === 'del') {
            del(obj);
        }
    });

    $(document).on('click','#add',function(){
        location.href = 'roleForm.html';
    });
    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        if(!params){
            params=common.from.serializeObject('#searchFrm');
        }
        roleTable.reload({ where: params})
    }

    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        layer.confirm('真的删除吗', function(index){
            console.log('Role Del param is', postData);
            common.ajax.post('/sys/role/delete', postData, function (resp) {
                searchTable();
                layer.close(index);
            })
        });
    }
});
